/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * checkEdit.java
 *
 * Created on 29-Sep-2010, 15:59:33
 */

package COMIC_BOOK_STORE;

import java.sql.*;
import javax.swing.JOptionPane;

/**
 *
 * @author benson
 */
public class MEMBER_CHECK_EDIT extends javax.swing.JFrame {

    /** Creates new form checkEdit */
    public MEMBER_CHECK_EDIT() {
        super("CHECK AND EDIT MEMBER");

        initComponents();
        this.setLocationRelativeTo(null);
        infoPanel.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        infoPanel = new javax.swing.JPanel();
        nameLabel = new javax.swing.JLabel();
        addressLabel = new javax.swing.JLabel();
        contactNumberLabel = new javax.swing.JLabel();
        DOBLabel = new javax.swing.JLabel();
        icNumberLabel = new javax.swing.JLabel();
        creditBalanceLabel = new javax.swing.JLabel();
        stateLabel = new javax.swing.JLabel();
        houseNumberLabel = new javax.swing.JLabel();
        postCodeLabel = new javax.swing.JLabel();
        gardenLabel = new javax.swing.JLabel();
        cityLabel = new javax.swing.JLabel();
        roadLabel = new javax.swing.JLabel();
        displayName = new javax.swing.JLabel();
        displayContactNumber = new javax.swing.JLabel();
        displayDOB = new javax.swing.JLabel();
        displayCreditBalance = new javax.swing.JLabel();
        displayHouseNumber = new javax.swing.JLabel();
        displayRoad = new javax.swing.JLabel();
        displayGarden = new javax.swing.JLabel();
        displayCity = new javax.swing.JLabel();
        displayPostCode = new javax.swing.JLabel();
        displayState = new javax.swing.JLabel();
        displayIcNumber = new javax.swing.JLabel();
        editButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel8 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        rentOutDateTextField2 = new javax.swing.JTextField();
        chargeTextField2 = new javax.swing.JTextField();
        bookNameTextField2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        enterButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        jLabel16 = new javax.swing.JLabel();
        jTextField24 = new javax.swing.JTextField();
        jTextField16 = new javax.swing.JTextField();
        jLabel71 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        infoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        infoPanel.setLayout(null);

        nameLabel.setText("First Name");
        infoPanel.add(nameLabel);
        nameLabel.setBounds(16, 17, 51, 14);

        addressLabel.setText("Address");
        infoPanel.add(addressLabel);
        addressLabel.setBounds(279, 49, 39, 14);

        contactNumberLabel.setText("Contact Number");
        infoPanel.add(contactNumberLabel);
        contactNumberLabel.setBounds(16, 90, 78, 14);

        DOBLabel.setText("D.O.B");
        infoPanel.add(DOBLabel);
        DOBLabel.setBounds(16, 110, 29, 14);

        icNumberLabel.setText("IC no.");
        infoPanel.add(icNumberLabel);
        icNumberLabel.setBounds(16, 130, 30, 14);

        creditBalanceLabel.setText("Credit Balance");
        infoPanel.add(creditBalanceLabel);
        creditBalanceLabel.setBounds(16, 150, 69, 14);

        stateLabel.setText("State");
        infoPanel.add(stateLabel);
        stateLabel.setBounds(298, 173, 26, 14);

        houseNumberLabel.setText("Apartment/House No");
        infoPanel.add(houseNumberLabel);
        houseNumberLabel.setBounds(298, 69, 101, 14);

        postCodeLabel.setText("Post code");
        infoPanel.add(postCodeLabel);
        postCodeLabel.setBounds(298, 150, 47, 14);

        gardenLabel.setText("Complex/Garden");
        infoPanel.add(gardenLabel);
        gardenLabel.setBounds(298, 109, 80, 14);

        cityLabel.setText("City");
        infoPanel.add(cityLabel);
        cityLabel.setBounds(298, 129, 19, 14);

        roadLabel.setText("Street/Road");
        infoPanel.add(roadLabel);
        roadLabel.setBounds(298, 89, 59, 14);

        displayName.setText("Benson");
        infoPanel.add(displayName);
        displayName.setBounds(141, 17, 35, 46);

        displayContactNumber.setText("016 - 998 3586");
        infoPanel.add(displayContactNumber);
        displayContactNumber.setBounds(141, 90, 73, 14);

        displayDOB.setText("30 Jun 1989");
        infoPanel.add(displayDOB);
        displayDOB.setBounds(141, 110, 59, 14);

        displayCreditBalance.setText("RM 54.40");
        infoPanel.add(displayCreditBalance);
        displayCreditBalance.setBounds(141, 150, 46, 14);

        displayHouseNumber.setText("Block 97 - 14 - 8, University Height");
        infoPanel.add(displayHouseNumber);
        displayHouseNumber.setBounds(417, 69, 166, 14);

        displayRoad.setText("Jalan Sungai Dua");
        infoPanel.add(displayRoad);
        displayRoad.setBounds(417, 89, 82, 14);

        displayGarden.setText("Taman Bukit Kiara");
        infoPanel.add(displayGarden);
        displayGarden.setBounds(417, 109, 85, 14);

        displayCity.setText("Kuala Lumpur");
        infoPanel.add(displayCity);
        displayCity.setBounds(417, 129, 64, 14);

        displayPostCode.setText("52200");
        infoPanel.add(displayPostCode);
        displayPostCode.setBounds(417, 150, 30, 14);

        displayState.setText("Wilayah Persekutuan");
        infoPanel.add(displayState);
        displayState.setBounds(417, 173, 101, 14);

        displayIcNumber.setText("889911-22-2233");
        infoPanel.add(displayIcNumber);
        displayIcNumber.setBounds(141, 130, 80, 14);

        editButton.setText("EDIT");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });
        infoPanel.add(editButton);
        editButton.setBounds(720, 350, 95, 34);

        jLabel1.setText("Last Name");
        infoPanel.add(jLabel1);
        jLabel1.setBounds(16, 70, 50, 14);

        jLabel2.setText("Hill");
        infoPanel.add(jLabel2);
        jLabel2.setBounds(141, 70, 13, 14);

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder("CURRENT RENTAL"));
        jPanel8.setFont(new java.awt.Font("Tahoma", 0, 36));
        jPanel8.setLayout(null);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "", "", ""
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(195);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(100);
        jTable1.getColumnModel().getColumn(2).setResizable(false);

        jPanel8.add(jScrollPane1);
        jScrollPane1.setBounds(16, 57, 489, 108);

        rentOutDateTextField2.setText("Rent Out Date");
        rentOutDateTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rentOutDateTextField2ActionPerformed(evt);
            }
        });
        jPanel8.add(rentOutDateTextField2);
        rentOutDateTextField2.setBounds(255, 31, 116, 20);

        chargeTextField2.setText("Charge");
        jPanel8.add(chargeTextField2);
        chargeTextField2.setBounds(389, 31, 116, 20);

        bookNameTextField2.setText("Book Name");
        jPanel8.add(bookNameTextField2);
        bookNameTextField2.setBounds(16, 31, 221, 20);

        infoPanel.add(jPanel8);
        jPanel8.setBounds(16, 216, 530, 190);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGE_REPOSITORY/Background3 copy.jpg"))); // NOI18N
        jLabel4.setText("jLabel4");
        infoPanel.add(jLabel4);
        jLabel4.setBounds(-30, -30, 880, 440);

        infoPanel.setBounds(30, 60, 840, 410);
        jLayeredPane1.add(infoPanel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        enterButton.setText("ENTER");
        enterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enterButtonActionPerformed(evt);
            }
        });
        enterButton.setBounds(640, 20, 100, 30);
        jLayeredPane1.add(enterButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        backButton.setText("MAIN MENU");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });
        backButton.setBounds(750, 20, 100, 30);
        jLayeredPane1.add(backButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel16.setText("Member Code");
        jLabel16.setBounds(30, 20, 90, 20);
        jLayeredPane1.add(jLabel16, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextField24.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField24ActionPerformed(evt);
            }
        });
        jTextField24.setBounds(470, 20, 130, 30);
        jLayeredPane1.add(jTextField24, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextField16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField16ActionPerformed(evt);
            }
        });
        jTextField16.setBounds(140, 20, 130, 30);
        jLayeredPane1.add(jTextField16, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel71.setText("Member IC");
        jLabel71.setBounds(350, 20, 100, 20);
        jLayeredPane1.add(jLabel71, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGE_REPOSITORY/Background3 copy.jpg"))); // NOI18N
        jLabel3.setText("jLabel3");
        jLabel3.setBounds(0, -30, 900, 560);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 901, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 498, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>

    public static String member_code = "";

    private void jTextField16ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
}                                            

    private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            

        String query = "";
        
        if ((jTextField16.getText() == null ? "" == null : jTextField16.getText().equals(""))&&(jTextField24.getText() == null ? "" == null : jTextField24.getText().equals("")))
        {
            JOptionPane.showMessageDialog(null, "Please enter either search criteria", "ERROR!", JOptionPane.WARNING_MESSAGE); 
        }
        
        else//search field not blank
        {
            if (jTextField24.getText() == null ? "" == null : jTextField24.getText().equals("")) //if ic is empty, search by member code
            {
                String member_code = (String)jTextField16.getText().trim();
                query = "select * from MEMBER where MEMBER_CODE = '" + member_code + "'";
            }
            
            else //if ic is not empty, search by ic
            {
                String icNo = (String)jTextField24.getText().trim();
                query = "select * from MEMBER where MEMBER_ICNO = '" + icNo + "'";
            }

//      --------------------------------------------------------------------------------------------------

                    try
            {
                DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
                Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","javaproject","javaproject");;

//      --------------------------------------------------------------------------------------------------

                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(query);

//      --------------------------------------------------------------------------------------------------

                String Gender = "";
                String member_code = "";
                
                   while (rs.next())
                   {
                 jTextField16.setText(rs.getString("MEMBER_CODE").trim());
                 displayName.setText(rs.getString("MEMBER_FNAME").trim());
                 jLabel2.setText(rs.getString("MEMBER_LNAME").trim());
                 displayDOB.setText(rs.getString("MEMBER_DOB").trim());
                 displayContactNumber.setText(rs.getString("MEMBER_CONTACTNO").trim());
                 jTextField24.setText(rs.getString("MEMBER_ICNO").trim());
                 displayHouseNumber.setText(rs.getString("MEMBER_HOUSENO").trim());
                 displayRoad.setText(rs.getString("MEMBER_STREET").trim());
                 displayGarden.setText(rs.getString("MEMBER_GARDEN").trim());
                 displayCity.setText(rs.getString("MEMBER_CITY").trim());
                 displayPostCode.setText(rs.getString("MEMBER_POSTCODE").trim());
                 displayState.setText(rs.getString("MEMBER_STATE").trim());
                 displayCreditBalance.setText("RM " + rs.getString("MEMBER_CREDIT").trim());
                 Gender = rs.getString("MEMBER_GENDER");
                 member_code = rs.getString("MEMBER_CODE");
                   }

                if (Gender == null ? "" == null : Gender.equals(""))
                {
                    System.out.println("query is " + query);
                    JOptionPane.showMessageDialog(null, "No search result", "ERROR!", JOptionPane.WARNING_MESSAGE);
                    infoPanel.setVisible(false);
                }
                 else
                {
                    query = "select * from TRANSACTION where MEMBER_CODE = '" + member_code + "' and RETURN_DATE is null order by RENTOUT_DATE desc";
                    String[] book_code = {"","","","",""};

                    Statement stmt2 = con.createStatement();
                    ResultSet rs2 = stmt2.executeQuery(query);

                    for (int i = 0; i < 5; i++)
                    {   while(rs2.next())
                        {   jTable1.setValueAt(rs2.getString("RENTOUT_DATE"),i,1);
                            book_code[i] = rs2.getString("BOOK_CODE");}
                    }

                    int i = 0;
                    
                    book_code[0] = "1234";
                    book_code[1] = "1235";

                    //retrieve book name
                    while (book_code[i] == null ? "" != null : !book_code[i].equals(""))
                    {
                        query = "select BOOK_TITLE, BOOK_CHARGE from BOOK where BOOK_CODE = '" + book_code[i] + "'";

                        Statement stmt3 = con.createStatement();
                        ResultSet rs3 = stmt3.executeQuery(query);
                        

                        while(rs3.next())
                        {   jTable1.setValueAt(rs3.getString("BOOK_TITLE"),i,0);
                            jTable1.setValueAt(rs3.getString("BOOK_CHARGE"),i,2);}

                        i++;
                        if(i > 4)
                            break;
                    }//end retrieving book name

                    infoPanel.setVisible(true);
                    System.out.println("OK!");
                }
            
            }//end try

            catch (Exception e)
            {System.out.println("error occurred");
             JOptionPane.showMessageDialog(null, "No search result", "ERROR!", JOptionPane.WARNING_MESSAGE); 
            }

//      --------------------------------------------------------------------------------------------------
            
        }//end else
        
}                                           

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    new MAIN_MENU().setVisible(true);
        this.setVisible(false);
}                                          

    private void jTextField24ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
}                                            

    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

        member_code = jTextField16.getText();

//        MEMBER_EDIT.firstNameTextField.setText(displayName.getText());
//        MEMBER_EDIT.lastNameTextField.setText(jLabel2.getText());
//        MEMBER_EDIT.displayIcNumber.setText(displayIcNumber.getText());
//        MEMBER_EDIT.contactNumberTextField.setText(displayContactNumber.getText());


//        MEMBER_EDIT.dayComboBox
//        MEMBER_EDIT.monthComboBox
//        MEMBER_EDIT.yearComboBox
//        MEMBER_EDIT.maleRadioButton

//        MEMBER_EDIT.houseNumberTextField.setText(displayHouseNumber.getText());
//        MEMBER_EDIT.roadTextField.setText(displayRoad.getText());
//        MEMBER_EDIT.gardenTextField.setText(displayGarden.getText());
//        MEMBER_EDIT.cityTextField.setText(displayCity.getText());
//        MEMBER_EDIT.postCodeTextField.setText(displayPostCode.getText());
//        MEMBER_EDIT.StateTextField1.setText(displayState.getText());
                
        
        new MEMBER_EDIT().setVisible(true);
        this.setVisible(false);
    }                                          

    private void rentOutDateTextField2ActionPerformed(java.awt.event.ActionEvent evt) {                                                      
        // TODO add your handling code here:
}                                                     

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MEMBER_CHECK_EDIT().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel DOBLabel;
    private javax.swing.JLabel addressLabel;
    private javax.swing.JButton backButton;
    private javax.swing.JTextField bookNameTextField2;
    private javax.swing.JTextField chargeTextField2;
    private javax.swing.JLabel cityLabel;
    private javax.swing.JLabel contactNumberLabel;
    private javax.swing.JLabel creditBalanceLabel;
    private javax.swing.JLabel displayCity;
    private javax.swing.JLabel displayContactNumber;
    private javax.swing.JLabel displayCreditBalance;
    private javax.swing.JLabel displayDOB;
    private javax.swing.JLabel displayGarden;
    private javax.swing.JLabel displayHouseNumber;
    private javax.swing.JLabel displayIcNumber;
    private javax.swing.JLabel displayName;
    private javax.swing.JLabel displayPostCode;
    private javax.swing.JLabel displayRoad;
    private javax.swing.JLabel displayState;
    private javax.swing.JButton editButton;
    private javax.swing.JButton enterButton;
    private javax.swing.JLabel gardenLabel;
    private javax.swing.JLabel houseNumberLabel;
    private javax.swing.JLabel icNumberLabel;
    private javax.swing.JPanel infoPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel71;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField16;
    private javax.swing.JTextField jTextField24;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel postCodeLabel;
    private javax.swing.JTextField rentOutDateTextField2;
    private javax.swing.JLabel roadLabel;
    private javax.swing.JLabel stateLabel;
    // End of variables declaration

}
